package com.hk.blog.repository.jpa;

import com.hk.blog.domain.CategoryInfo;
import com.hk.core.data.jpa.repository.LongIdJpaRepository;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface CategoryInfoRepository extends LongIdJpaRepository<CategoryInfo> {

    boolean existsByUserIdAndCategoryName(Long userId, String categoryName);

    @Modifying
    @Query(value = "UPDATE CategoryInfo SET articleNum = articleNum - ?2 WHERE id = ?1")
    void subtractArticleNum(Long id, int num);

    List<CategoryInfo> findByUserId(Long userId, Sort sort);

    Optional<CategoryInfo> findByUserIdAndCategoryName(Long userId, String categoryName);

    @Modifying
    @Query(value = "UPDATE CategoryInfo SET articleNum = articleNum + ?2 WHERE id = ?1")
    void addArticleNum(Long id, int articleNum);
}
